package com.example.wiki.auth.service;


import com.example.wiki.auth.entity.Wikiuser;
import com.example.wiki.auth.entity.WikiuserExample;
import com.example.wiki.auth.exception.BusinessException;
import com.example.wiki.auth.exception.BusinessExceptionCode;

import com.example.wiki.mapper.WikiuserMapper;
import com.example.wiki.req.UserLoginReq;
import com.example.wiki.req.UserQueryReq;
import com.example.wiki.req.UserResetPasswordReq;
import com.example.wiki.req.UserSaveReq;
import com.example.wiki.resp.UserLoginResp;
import com.example.wiki.resp.UserQueryResp;
import com.example.wiki.resp.PageResp;
import com.example.wiki.utils.CopyUtil;
import com.example.wiki.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;


@Service
public class UserService {
    private static final Logger Log = LoggerFactory.getLogger(UserService.class);
    @Resource
    private WikiuserMapper userMapper;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 查询列表
     * @param req
     * @return
     */
    public PageResp<UserQueryResp> list(UserQueryReq req) {
        WikiuserExample wikiuserExample = new WikiuserExample();
        WikiuserExample.Criteria criteria = wikiuserExample.createCriteria();

        if (!ObjectUtils.isEmpty(req.getLoginName())) {
            criteria.andLoginNameEqualTo(req.getLoginName());
        }


        PageHelper.startPage(req.getPage(), req.getSize());
        List<Wikiuser> userList = userMapper.selectByExample(wikiuserExample);

        PageInfo<Wikiuser> pageInfo = new PageInfo<>(userList);
        Log.info("总行数"+pageInfo.getTotal());
        Log.info("总页数"+pageInfo.getPages());

//        List<UserResp> respList  =new ArrayList<>();
//        for (User user :userList){
//
//            UserResp userResp = new UserResp();
//            //最原始的设置属性值方法
////            userResp.setName(user.getName());
////            利用beanutil的简便方法
////            BeanUtils.copyProperties(user,userResp);
////            respList.add(userResp);
//        }
        //利用工具封装，简化上面代码

        List<UserQueryResp> respList = CopyUtil.copyList(userList, UserQueryResp.class);

        PageResp<UserQueryResp> pageResp =new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);

        return pageResp;
    }


        /**
         * 保存
         */


        public void save(@RequestBody UserSaveReq req) {
            System.out.println(req);
            Wikiuser user = CopyUtil.copy(req, Wikiuser.class);

            if (ObjectUtils.isEmpty(req.getId())) {
                Wikiuser userDb = selectByLoginName(req.getLoginName());
                //没有用户
                if(ObjectUtils.isEmpty(userDb)){
                    // 新增  自增 uuid 雪花
                    user.setId(snowFlake.nextId());
                    userMapper.insert(user);
                }else {
                    throw  new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST,403);
                }


                userMapper.insert(user);
            } else {
                // 更新
                user.setLoginName(null);
                user.setPassword(null);
                userMapper.updateByPrimaryKeySelective(user);
            }
        }
    public void save( Wikiuser user) {
        System.out.println(user);
        Log.info("保存用户信息",user);
        if (!ObjectUtils.isEmpty(user)) {
            Wikiuser userdb = selectByLoginName(user.getName());
            if (ObjectUtils.isEmpty(userdb)) {
                userMapper.insert(user);
                Log.info("用户插入数据库成功",user);
            } else {
                Log.info("用户更新数据库成功",user);
                // 更新
                user.setLoginName(null);
                user.setPassword(null);
                userMapper.updateByPrimaryKeySelective(user);
            }
        }else {
            Log.info("空用户名",user);
        }
    }



    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    //
    public Wikiuser selectByLoginName(String LoginName) {
        WikiuserExample userExample = new WikiuserExample();
        WikiuserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(LoginName);
        List<Wikiuser> userList = userMapper.selectByExample(userExample);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        } else {
            return userList.get(0);
        }
    }




    /**
     * 修改密码
     */
    public void resetPassword(UserResetPasswordReq req) {
        Wikiuser user = CopyUtil.copy(req, Wikiuser.class);
        userMapper.updateByPrimaryKeySelective(user);
    }


    /**
     * 登录
     */
    public UserLoginResp login(UserLoginReq req) {
        Wikiuser userDb = selectByLoginName(req.getLoginName());
        if (ObjectUtils.isEmpty(userDb)) {
            // 用户名不存在
            Log.info("用户名不存在, {}", req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR,401);
        } else {
            if (userDb.getPassword().equals(req.getPassword())) {
                // 登录成功
                UserLoginResp userLoginResp = CopyUtil.copy(userDb, UserLoginResp.class);
                return userLoginResp;
            } else {
                // 密码不对
                Log.info("密码不对, 输入密码：{}, 数据库密码：{}", req.getPassword(), userDb.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR,401);
            }
        }
    }












}

